<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="format-detection" content="telephone=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="icon" href="/images/touxiang1.png?v=2.6.1" type="image/png" sizes="16x16"><link rel="icon" href="/images/touxiang1.png?v=2.6.1" type="image/png" sizes="32x32"><meta name="description" content="1.反向代理:         反向代理:                       2.负载均衡                             3.动静分离                               4.Nginx安装                           1.查看IP地址       使用xshell 或其他远">
<meta property="og:type" content="article">
<meta property="og:title" content="Nginx">
<meta property="og:url" content="https://gityang2016.gitee.io/2021/03/22/ngnix_01/index.html">
<meta property="og:site_name" content="Yangj&#39;s blog">
<meta property="og:description" content="1.反向代理:         反向代理:                       2.负载均衡                             3.动静分离                               4.Nginx安装                           1.查看IP地址       使用xshell 或其他远">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx1.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx2.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx3.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx4.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx5.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx6.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx7.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx8.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx9.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx10.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx11.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx12.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx13.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx16.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx14.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx15.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx17.png">
<meta property="og:image" content="https://gityang2016.gitee.io/images/nginx/nginx18.png">
<meta property="og:image" content="http://gityang2016.gitee.io/my-images/nginx/nginx20.png">
<meta property="og:image" content="http://gityang2016.gitee.io/my-images/nginx/nginx21.png">
<meta property="og:image" content="http://gityang2016.gitee.io/my-images/nginx/nginx22.png">
<meta property="og:image" content="http://gityang2016.gitee.io/my-images/nginx/nginx23.png">
<meta property="og:image" content="http://gityang2016.gitee.io/my-images/nginx/nginx24.png">
<meta property="og:image" content="http://gityang2016.gitee.io/my-images/nginx/nginx25.jpg">
<meta property="og:image" content="http://gityang2016.gitee.io/my-images/nginx/nginx26.jpg">
<meta property="og:image" content="http://gityang2016.gitee.io/my-images/nginx/nginx27.jpg">
<meta property="og:image" content="http://gityang2016.gitee.io/my-images/nginx/nginx28.jpg">
<meta property="og:image" content="http://gityang2016.gitee.io/my-images/nginx/nginx29.jpg">
<meta property="og:image" content="http://gityang2016.gitee.io/my-images/nginx/nginx30.png">
<meta property="og:image" content="http://gityang2016.gitee.io/my-images/nginx/nginx31.png">
<meta property="og:image" content="http://gityang2016.gitee.io/my-images/nginx/nginx34.jpg">
<meta property="og:image" content="http://gityang2016.gitee.io/my-images/nginx/nginx32.jpg">
<meta property="og:image" content="http://gityang2016.gitee.io/my-images/nginx/nginx33.jpg">
<meta property="article:published_time" content="2021-03-22T08:04:37.982Z">
<meta property="article:modified_time" content="2021-03-22T13:54:56.218Z">
<meta property="article:author" content="Yangj">
<meta property="article:tag" content="Nginx">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gityang2016.gitee.io/images/nginx/nginx1.png"><title>Nginx | Yangj's blog</title><link ref="canonical" href="https://gityang2016.gitee.io/2021/03/22/ngnix_01/"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css" type="text/css"><link rel="stylesheet" href="/css/index.css?v=2.6.1"><script>var Stun = window.Stun || {};
var CONFIG = {
  root: '/',
  algolia: undefined,
  assistSearch: undefined,
  fontIcon: {"prompt":{"success":"fas fa-check-circle","info":"fas fa-arrow-circle-right","warning":"fas fa-exclamation-circle","error":"fas fa-times-circle"},"copyBtn":"fas fa-copy"},
  sidebar: {"offsetTop":"20px","tocMaxDepth":6},
  header: {"enable":true,"showOnPost":true,"scrollDownIcon":true},
  postWidget: {"endText":true},
  nightMode: {"enable":true},
  back2top: {"enable":true},
  codeblock: {"style":"default","highlight":"light","wordWrap":false},
  reward: false,
  fancybox: false,
  zoomImage: {"gapAside":"20px"},
  galleryWaterfall: undefined,
  lazyload: false,
  pjax: undefined,
  externalLink: {"icon":{"enable":true,"name":"fas fa-external-link-alt"}},
  shortcuts: undefined,
  prompt: {"copyButton":"Copy","copySuccess":"Copy Success","copyError":"Copy Error"},
  sourcePath: {"js":"js","css":"css","images":"images"},
};

window.CONFIG = CONFIG;</script><meta name="generator" content="Hexo 5.3.0"><link rel="alternate" href="/atom.xml" title="Yangj's blog" type="application/atom+xml">
</head><body><div class="container" id="container"><header class="header" id="header"><div class="header-inner"><nav class="header-nav header-nav--fixed"><div class="header-nav-inner"><div class="header-nav-menubtn"><i class="fas fa-bars"></i></div><div class="header-nav-menu"><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/"><span class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span class="header-nav-menu-item__text">Home</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/archives/"><span class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span class="header-nav-menu-item__text">Archives</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/categories/"><span class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span class="header-nav-menu-item__text">Categories</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/tags/"><span class="header-nav-menu-item__icon"><i class="fas fa-tags"></i></span><span class="header-nav-menu-item__text">Tags</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/aboutme/"><span class="header-nav-menu-item__icon"><i class="fas fa-user-tag"></i></span><span class="header-nav-menu-item__text">About</span></a></div></div><div class="header-nav-mode"><div class="mode"><div class="mode-track"><span class="mode-track-moon"></span><span class="mode-track-sun"></span></div><div class="mode-thumb"></div></div></div></div></nav><div class="header-banner"><div class="header-banner-info"><div class="header-banner-info__title">Yangj's blog</div><div class="header-banner-info__subtitle">It is never too late to mend</div></div><div class="header-banner-arrow"><div class="header-banner-arrow__icon"><i class="fas fa-angle-down"></i></div></div></div></div></header><main class="main" id="main"><div class="main-inner"><div class="content-wrap" id="content-wrap"><div class="content" id="content"><!-- Just used to judge whether it is an article page--><div id="is-post"></div><div class="post"><header class="post-header"><h1 class="post-title">Nginx</h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">Created</span><span class="post-meta-item__value">2021-03-22</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">Updated</span><span class="post-meta-item__value">2021-03-22</span></span></div></header><div class="post-body">
        <h1 id="1-反向代理"   >
          <a href="#1-反向代理" class="heading-link"><i class="fas fa-link"></i></a>1.反向代理:</h1>
      <p><img src="/images/nginx/nginx1.png"> </p>
<p>反向代理:</p>
<p><img src="/images/nginx/nginx2.png"> </p>

        <h1 id="2-负载均衡"   >
          <a href="#2-负载均衡" class="heading-link"><i class="fas fa-link"></i></a>2.负载均衡</h1>
      <p><img src="/images/nginx/nginx3.png"> </p>

        <h1 id="3-动静分离"   >
          <a href="#3-动静分离" class="heading-link"><i class="fas fa-link"></i></a>3.动静分离</h1>
      <p><img src="/images/nginx/nginx4.png"> </p>
<p><img src="/images/nginx/nginx5.png"> </p>

        <h1 id="4-Nginx安装"   >
          <a href="#4-Nginx安装" class="heading-link"><i class="fas fa-link"></i></a>4.Nginx安装</h1>
      
        <h2 id="1-查看IP地址"   >
          <a href="#1-查看IP地址" class="heading-link"><i class="fas fa-link"></i></a>1.查看IP地址</h2>
      <p>使用xshell 或其他远程连接工具</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ifconfig</span><br></pre></td></tr></table></div></figure>
<p>安装编译工具,用root用户</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel</span><br></pre></td></tr></table></div></figure>

        <h2 id="2-pcre安装"   >
          <a href="#2-pcre安装" class="heading-link"><i class="fas fa-link"></i></a>2.pcre安装</h2>
      <p>下载地址:<br><span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://udomain.dl.sourceforge.net/project/pcre/pcre2/10.36/pcre2-10.36.zip" >https://udomain.dl.sourceforge.net/project/pcre/pcre2/10.36/pcre2-10.36.zip</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>解压</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">unzip pcre2-10.36.zip</span><br></pre></td></tr></table></div></figure>
<p>进入pcre目录，执行 ./configure</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj pcre2-10.36]$ .&#x2F;configure</span><br></pre></td></tr></table></div></figure>
<p>编译安装</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj pcre2-10.36]$ make &amp;&amp; make install</span><br></pre></td></tr></table></div></figure>
<p>查看是否安装成功</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj pcre2-10.36]# pcre-config --version</span><br></pre></td></tr></table></div></figure>

        <h2 id="3-nginx-安装"   >
          <a href="#3-nginx-安装" class="heading-link"><i class="fas fa-link"></i></a>3.nginx 安装</h2>
      <p>下载<br><span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://nginx.org/download/nginx-1.18.0.tar.gz" >nginx-1.18.0.tar.gz</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span><br>解压</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar -xvf nginx-1.18.0.tar.gz</span><br></pre></td></tr></table></div></figure>
<p>进入解压目录，执行./configure</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj nginx-1.18.0]$ .&#x2F;configure</span><br></pre></td></tr></table></div></figure>
<p>编译安装</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj nginx-1.18.0]$ make &amp;&amp; make all</span><br></pre></td></tr></table></div></figure>
<p>查看版本</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj sbin]# &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin&#x2F;nginx -v</span><br><span class="line">nginx version: nginx&#x2F;1.18.0</span><br></pre></td></tr></table></div></figure>
<p>查看防火墙命令:</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj ~]# firewall-cmd --list-all</span><br></pre></td></tr></table></div></figure>
<p>开放80端口</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj ~]# firewall-cmd --add-service&#x3D;http --permanent</span><br><span class="line">[root@yangj ~]# firewall-cmd --add-port&#x3D;80&#x2F;tcp --permanent</span><br></pre></td></tr></table></div></figure>
<p>重启</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj ~]# firewall-cmd --reload</span><br></pre></td></tr></table></div></figure>
<p>成功:</p>
<p><img src="/images/nginx/nginx6.png"> </p>
<p>页面访问<br><img src="/images/nginx/nginx7.png"> </p>

        <h1 id="5-Nginx反向代理示例一"   >
          <a href="#5-Nginx反向代理示例一" class="heading-link"><i class="fas fa-link"></i></a>5.Nginx反向代理示例一:</h1>
      <p>打开浏览器，在浏览器地址栏输入地址  tomcat.com，跳转到 liunx 系统 tomcat 主页面中。</p>

<p><span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="https://mirrors.bfsu.edu.cn/apache/tomcat/tomcat-8/v8.5.64/bin/apache-tomcat-8.5.64.tar.gz" >Tomcat下载</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>
<p>新建文件夹，解压Tomcat安装包</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[yang@yangj tomcat8080]$ tar -xvf apache-tomcat-8.5.64.tar.gz</span><br></pre></td></tr></table></div></figure>
<p>启动Tomcat</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[yang@yangj tomcat8080]$ cd apache-tomcat-8.5.64&#x2F;bin</span><br><span class="line">[yang@yangj bin]$ .&#x2F;startup.sh</span><br></pre></td></tr></table></div></figure>
<p>开放8080端口</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj ~]# firewall-cmd --add-port&#x3D;8080&#x2F;tcp --permanent</span><br></pre></td></tr></table></div></figure>
<p>重启</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj ~]# firewall-cmd --reload</span><br></pre></td></tr></table></div></figure>
<p>成功界面<br><img src="/images/nginx/nginx8.png"> </p>
<p>主机host,文件修改。把域名(如:tomcat.com)转发到对应IP(192.168.76.130)<br>C:\Windows\System32\drivers\etc\hosts 文件</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">192.168.76.130  tomcat.com</span><br></pre></td></tr></table></div></figure>
<p>修改nginx配置文件 vi /usr/local/nginx/conf/nginx.conf</p>
<p><img src="/images/nginx/nginx9.png"> </p>
<p>重启Nginx</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin</span><br><span class="line">.&#x2F;nginx -s reload</span><br></pre></td></tr></table></div></figure>
<p>结果页:</p>
<p><img src="/images/nginx/nginx10.png"> </p>
<p>Tip： 通过nginx 把 tomcat.com 请求转发到  <span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://192.168.76.130:8080/" >http://192.168.76.130:8080</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>

        <h1 id="6-Nginx反向代理示例二"   >
          <a href="#6-Nginx反向代理示例二" class="heading-link"><i class="fas fa-link"></i></a>6.Nginx反向代理示例二:</h1>
      <p>使用使用 nginx 反向代理，根据访问的路径跳转到不同端口的服务中(Tomcat) 
nginx 监听端口为 9001， 
访问  http://192.168.17.129:9001/edu/  直接跳转到 127.0.0.1:8080 
访问  http:// 192.168.17.129:9001/vod/  直接跳转到 127.0.0.1:8081 
</p>
配置8081,Tomcat
在一个新的文件夹解压Tomcat
apache-tomcat-8.5.64/conf/server.xml文件

<p><img src="/images/nginx/nginx11.png"> </p>
<p>启动Tomcat</p>
<p><img src="/images/nginx/nginx12.png"> </p>
<p>开放8081,9001端口</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj ~]# firewall-cmd --add-port&#x3D;8081&#x2F;tcp --permanent</span><br><span class="line">[root@yangj ~]# firewall-cmd --add-port&#x3D;9001&#x2F;tcp --permanent</span><br></pre></td></tr></table></div></figure>
<p>重启</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[root@yangj ~]# firewall-cmd --reload</span><br></pre></td></tr></table></div></figure>
<p>8081能访问</p>
<p><img src="/images/nginx/nginx13.png"> </p>
<p><strong>nginx配置</strong></p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">server &#123;</span><br><span class="line">           listen 9001;</span><br><span class="line"></span><br><span class="line">           location ~&#x2F;edu&#x2F; &#123;</span><br><span class="line">                proxy_pass  http:&#x2F;&#x2F;127.0.0.1:8080;</span><br><span class="line">           &#125;    </span><br><span class="line">        </span><br><span class="line">           location ~&#x2F;vod&#x2F; &#123;</span><br><span class="line">                proxy_pass  http:&#x2F;&#x2F;127.0.0.1:8081;</span><br><span class="line">           &#125;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></div></figure>
<p><img src="/images/nginx/nginx16.png"> </p>
<p>重启Nginx</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin</span><br><span class="line">.&#x2F;nginx -s reload</span><br></pre></td></tr></table></div></figure>
<p>创建测试文件</p>
<p><img src="/images/nginx/nginx14.png"> </p>
<p><img src="/images/nginx/nginx15.png"> </p>
<p><strong>测试结果:</strong></p>
<p><img src="/images/nginx/nginx17.png"> </p>
<p><img src="/images/nginx/nginx18.png"> </p>

        <h1 id="7-Nginx-配置实例-负载均衡"   >
          <a href="#7-Nginx-配置实例-负载均衡" class="heading-link"><i class="fas fa-link"></i></a>7.Nginx 配置实例-负载均衡</h1>
      <p>1、实现效果<br>（1）浏览器地址栏输入地址  <span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://192.168.76.130/edu/a.html%EF%BC%8C%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1%E6%95%88%E6%9E%9C%EF%BC%8C%E5%B9%B3%E5%9D%87" >http://192.168.76.130/edu/a.html，负载均衡效果，平均</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span> 到8080<br>和 8081 端口中 </p>
<p>2、准备工作<br>（1）准备两台 tomcat 服务器，一台 8080，一台 8081<br>（2）在两台 tomcat 里面 webapps 目录中，创建名称是 edu 文件夹，在 edu 文件夹中创建<br>页面 a.html，用于测试 </p>
<p>3、在 nginx 的配置文件中进行负载均衡的配置 </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx20.png"></p>
<p>结果:</p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx21.png"></p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx22.png"></p>
<p>4、nginx 分配服务器策略<br>第一种  轮询（默认）<br>每个请求按时间顺序逐一分配到不同的后端服务器，如果后端服务器 down 掉，能自动剔除。 </p>
<p>第二种 weight<br>weight 代表权重默认为 1,权重越高被分配的客户端越多 </p>
<p>第三种 ip_hash<br>每个请求按访问 ip 的 hash 结果分配，这样每个访客固定访问一个后端服务器 </p>
<p>第四种 fair（第三方）<br>按后端服务器的响应时间来分配请求，响应时间短的优先分配。 </p>

        <h1 id="8-Nginx-配置实例-动静分离"   >
          <a href="#8-Nginx-配置实例-动静分离" class="heading-link"><i class="fas fa-link"></i></a>8.Nginx 配置实例-动静分离</h1>
      <p>1、什么是动静分离 </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx23.png"></p>
<p>通过  location  指定不同的后缀名实现不同的请求转发。通过  expires  参数设置，可以使浏<br>览器缓存过期时间，减少与服务器之前的请求和流量。具体  Expires  定义：是给一个资源<br>设定一个过期时间，也就是说无需去服务端验证，直接通过浏览器自身确认是否过期即可，<br>所以不会产生额外的流量。此种方法非常适合不经常变动的资源。（如果经常更新的文件，<br>不建议使用  Expires  来缓存），我这里设置  3d，表示在这  3  天之内访问这个  URL，发送一<br>个请求，比对服务器该文件最后更新时间没有变化，则不会从服务器抓取，返回状态码  304，<br>如果有修改，则直接从服务器重新下载，返回状态码  200。 </p>
<p>2、准备工作<br>（1）在 liunx 系统中准备静态资源，用于进行访问 </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx24.png"></p>
<p>3、具体配置<br>（1）在 nginx 配置文件中进行配置 </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx25.jpg"></p>
<p>4、最终测试<br>（1）浏览器中输入地址<br><span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://192.168.76.130/images/1.jpg" >http://192.168.76.130/images/1.jpg</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx26.jpg"></p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx27.jpg"></p>
<p>（2）在浏览器地址栏输入地址<br><span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://192.168.76.130/www/a.html" >http://192.168.76.130/www/a.html</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span></p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx28.jpg"></p>

        <h1 id="9-Nginx-配置高可用的集群"   >
          <a href="#9-Nginx-配置高可用的集群" class="heading-link"><i class="fas fa-link"></i></a>9.Nginx 配置高可用的集群</h1>
      <p>1、什么是 nginx 高可用 </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx29.jpg"></p>
<p>2、配置高可用的准备工作<br>（1）需要两台服务器  192.168.76.130  和  192.168.76.131<br>（2）在两台服务器安装 nginx<br>（3）在两台服务器安装 keepalived </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx30.png"></p>
<p>3、在两台服务器安装 keepalived s<br>（1）使用 yum 命令进行安装 </p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install keepalived –y </span><br></pre></td></tr></table></div></figure>
<p>（2）安装之后，在 etc 里面生成目录 keepalived，有文件 keepalived.conf </p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl start keepalived.service</span><br></pre></td></tr></table></div></figure>
<p>4、完成高可用配置（主从配置）<br>（1）修改/etc/keepalived/keepalivec.conf 配置文件 </p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">global_defs &#123; </span><br><span class="line">      notification_email &#123; </span><br><span class="line">          acassen@firewall.loc </span><br><span class="line">          failover@firewall.loc </span><br><span class="line">          sysadmin@firewall.loc </span><br><span class="line">      &#125; </span><br><span class="line">      notification_email_from Alexandre.Cassen@firewall.loc </span><br><span class="line">      smtp_server 192.168.76.131   #ip地址</span><br><span class="line">      smtp_connect_timeout 30 </span><br><span class="line">      router_id LVS_DEVEL </span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">vrrp_script chk_http_port &#123; </span><br><span class="line"></span><br><span class="line">      script &quot;&#x2F;usr&#x2F;local&#x2F;src&#x2F;nginx_check.sh&quot; </span><br><span class="line">       </span><br><span class="line">      interval 2            #（检测脚本执行的间隔） </span><br><span class="line">       </span><br><span class="line">      weight 2 </span><br><span class="line"></span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">vrrp_instance VI_1 &#123; </span><br><span class="line">        state BACKUP      #  备份服务器上将  MASTER  改为  BACKUP     </span><br><span class="line">        interface ens33    &#x2F;&#x2F;网卡 </span><br><span class="line">        virtual_router_id 51      #  主、备机的 virtual_router_id 必须相同 </span><br><span class="line">        priority 80          #  主、备机取不同的优先级，主机值较大，备份机值较小 </span><br><span class="line">        advert_int 1 </span><br><span class="line">	authentication &#123; </span><br><span class="line">	auth_type PASS </span><br><span class="line">	auth_pass 1111 </span><br><span class="line">        &#125; </span><br><span class="line">        virtual_ipaddress &#123; </span><br><span class="line">                192.168.76.50 &#x2F;&#x2F; VRRP H 虚拟地址 </span><br><span class="line">        &#125;</span><br><span class="line">	track_script &#123;</span><br><span class="line">         chk_http_port</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>
<p>（2）在/usr/local/src 添加检测脚本 </p>
<p>/usr/local/src/nginx_check.sh,注意编码格式:Unix(LF)</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">A&#x3D;&#96;ps -C nginx --no-header |wc -l&#96;</span><br><span class="line">if [ $A -eq 0 ];then</span><br><span class="line">    &#x2F;usr&#x2F;local&#x2F;nginx&#x2F;sbin&#x2F;nginx</span><br><span class="line">    sleep 2</span><br><span class="line">    if [ &#96;ps -C nginx --no-header |wc -l&#96; -eq 0 ];then</span><br><span class="line">        killall keepalived</span><br><span class="line">    fi</span><br><span class="line">fi</span><br></pre></td></tr></table></div></figure>
<p>（3）把两台服务器上 nginx 和 keepalived 启动<br>启动 nginx：./nginx<br>启动 keepalived：systemctl start keepalived.service </p>
<p>5、最终测试<br>（1）在浏览器地址栏输入  虚拟 ip 地址 <span class="exturl"><a class="exturl__link"   target="_blank" rel="noopener" href="http://192.168.76.50/" >http://192.168.76.50/</a><span class="exturl__icon"><i class="fas fa-external-link-alt"></i></span></span>   </p>
<p>（2）把主服务器（192.168.76.130）nginx 和 keepalived 停止，再输入 192.168.76.50 </p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl stop keepalived.service </span><br></pre></td></tr></table></div></figure>
<p>/usr/local/nginx/sbin</p>
<figure class="highlight plain"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.&#x2F;nginx -s stop</span><br></pre></td></tr></table></div></figure>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx31.png"></p>

        <h1 id="10-Nginx-的原理"   >
          <a href="#10-Nginx-的原理" class="heading-link"><i class="fas fa-link"></i></a>10.Nginx 的原理</h1>
      <p>1、mater 和 worker </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx34.jpg"></p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx32.jpg"></p>
<p>2、worker 如何进行工作的 </p>
<p><img src="http://gityang2016.gitee.io/my-images/nginx/nginx33.jpg"></p>
<p>3、一个 master 和多个 woker 有好处<br>（1）可以使用 nginx –s reload  热部署，利用 nginx 进行热部署操作<br>（2）每个 woker 是独立的进程，如果有其中的一个 woker 出现问题，其他 woker 独立的，<br>继续进行争抢，实现请求过程，不会造成服务中断 </p>
<p>4、设置多少个 woker 合适 </p>
<p>worker 数和服务器的 cpu 数相等是最为适宜的 </p>
<p>5、连接数 worker_connection<br>第一个：发送请求，占用了 woker 的几个连接数？<br>答案：2 或者 4 个 </p>
<p>第二个：nginx 有一个 master，有四个 woker，每个 woker 支持最大的连接数 1024，支持的<br>最大并发数是多少？<br>  普通的静态访问最大并发数是：  worker_connections * worker_processes /2，<br>  而如果是 HTTP 作  为反向代理来说，最大并发数量应该是 worker_connections *<br>worker_processes/4。 </p>
</div><footer class="post-footer"><div class="post-ending ending"><div class="ending__text">------ END ------</div></div><div class="post-copyright copyright"><div class="copyright-author"><span class="copyright-author__name">Author: </span><span class="copyright-author__value"><a href="https://gityang2016.gitee.io">Yangj</a></span></div><div class="copyright-link"><span class="copyright-link__name">Link: </span><span class="copyright-link__value"><a href="https://gityang2016.gitee.io/2021/03/22/ngnix_01/">https://gityang2016.gitee.io/2021/03/22/ngnix_01/</a></span></div><div class="copyright-notice"><span class="copyright-notice__name">Copyright: </span><span class="copyright-notice__value">All articles in this blog are licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" rel="external nofollow" target="_blank">BY-NC-SA</a> unless stating additionally</span></div></div><div class="post-tags"><span class="post-tags-item"><span class="post-tags-item__icon"><i class="fas fa-tag"></i></span><a class="post-tags-item__link" href="https://gityang2016.gitee.io/tags/Nginx/">Nginx</a></span></div><nav class="post-paginator paginator"><div class="paginator-next"><a class="paginator-next__link" href="/2021/01/27/java_annotation_01/"><span class="paginator-prev__text">反射</span><span class="paginator-next__icon"><i class="fas fa-angle-right"></i></span></a></div></nav></footer></div></div></div><div class="sidebar-wrap" id="sidebar-wrap"><aside class="sidebar" id="sidebar"><div class="sidebar-nav"><span class="sidebar-nav-toc current">Catalog</span><span class="sidebar-nav-ov">Overview</span></div><section class="sidebar-toc"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86"><span class="toc-number">1.</span> <span class="toc-text">
          1.反向代理:</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1"><span class="toc-number">2.</span> <span class="toc-text">
          2.负载均衡</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB"><span class="toc-number">3.</span> <span class="toc-text">
          3.动静分离</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-Nginx%E5%AE%89%E8%A3%85"><span class="toc-number">4.</span> <span class="toc-text">
          4.Nginx安装</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E6%9F%A5%E7%9C%8BIP%E5%9C%B0%E5%9D%80"><span class="toc-number">4.1.</span> <span class="toc-text">
          1.查看IP地址</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-pcre%E5%AE%89%E8%A3%85"><span class="toc-number">4.2.</span> <span class="toc-text">
          2.pcre安装</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-nginx-%E5%AE%89%E8%A3%85"><span class="toc-number">4.3.</span> <span class="toc-text">
          3.nginx 安装</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-Nginx%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E7%A4%BA%E4%BE%8B%E4%B8%80"><span class="toc-number">5.</span> <span class="toc-text">
          5.Nginx反向代理示例一:</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-Nginx%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86%E7%A4%BA%E4%BE%8B%E4%BA%8C"><span class="toc-number">6.</span> <span class="toc-text">
          6.Nginx反向代理示例二:</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#7-Nginx-%E9%85%8D%E7%BD%AE%E5%AE%9E%E4%BE%8B-%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1"><span class="toc-number">7.</span> <span class="toc-text">
          7.Nginx 配置实例-负载均衡</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#8-Nginx-%E9%85%8D%E7%BD%AE%E5%AE%9E%E4%BE%8B-%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB"><span class="toc-number">8.</span> <span class="toc-text">
          8.Nginx 配置实例-动静分离</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#9-Nginx-%E9%85%8D%E7%BD%AE%E9%AB%98%E5%8F%AF%E7%94%A8%E7%9A%84%E9%9B%86%E7%BE%A4"><span class="toc-number">9.</span> <span class="toc-text">
          9.Nginx 配置高可用的集群</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#10-Nginx-%E7%9A%84%E5%8E%9F%E7%90%86"><span class="toc-number">10.</span> <span class="toc-text">
          10.Nginx 的原理</span></a></li></ol></section><!-- ov = overview--><section class="sidebar-ov hide"><div class="sidebar-ov-author"><div class="sidebar-ov-author__avatar"><img class="sidebar-ov-author__avatar_img" src="/images/touxiang2.jpg" alt="avatar"></div><p class="sidebar-ov-author__text">Storms make trees take deeper roots</p></div><div class="sidebar-ov-social"><a class="sidebar-ov-social-item" href="https://github.com/Gityang2016" target="_blank" rel="noopener" data-popover="Github" data-popover-pos="up"><span class="sidebar-ov-social-item__icon"><i class="fab fa-github"></i></span></a></div><div class="sidebar-ov-state"><a class="sidebar-ov-state-item sidebar-ov-state-item--posts" href="/archives/"><div class="sidebar-ov-state-item__count">6</div><div class="sidebar-ov-state-item__name">Archives</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--categories" href="/categories/"><div class="sidebar-ov-state-item__count">8</div><div class="sidebar-ov-state-item__name">Categories</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--tags" href="/tags/"><div class="sidebar-ov-state-item__count">5</div><div class="sidebar-ov-state-item__name">Tags</div></a></div><div class="sidebar-ov-cc"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" target="_blank" rel="noopener" data-popover="Creative Commons" data-popover-pos="up"><img src="/images/cc-by-nc-sa.svg"></a></div></section><div class="sidebar-reading"><div class="sidebar-reading-info"><span class="sidebar-reading-info__text">You have read </span><span class="sidebar-reading-info__num">0</span><span class="sidebar-reading-info__perc">%</span></div><div class="sidebar-reading-line"></div></div></aside></div><div class="clearfix"></div></div></main><footer class="footer" id="footer"><div class="footer-inner"><div><span>Copyright © 2021</span><span class="footer__icon"><i class="fas fa-heart"></i></span><span>Gityang2016</span></div><div><span>Powered by <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a></span><span> v5.3.0</span><span class="footer__devider">|</span><span>Theme - <a href="https://github.com/liuyib/hexo-theme-stun/" title="Stun" target="_blank" rel="noopener">Stun</a></span><span> v2.6.1</span></div></div></footer><div class="loading-bar" id="loading-bar"><div class="loading-bar__progress"></div></div><div class="back2top" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span></div></div><script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script><script src="/js/utils.js?v=2.6.1"></script><script src="/js/stun-boot.js?v=2.6.1"></script><script src="/js/scroll.js?v=2.6.1"></script><script src="/js/header.js?v=2.6.1"></script><script src="/js/sidebar.js?v=2.6.1"></script></body></html>